package Utils

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/levigross/grequests"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/url"
	"strconv"
	"time"
)

type SecurityAlarmParam struct {
	StartTime      string `json:"startTime"`      // 开始时间
	EndTime        string `json:"endTime"`        // 结束时间
	CurPage        int    `json:"curPage"`        // 页数
	PageSize       int    `json:"pageSize"`       // 页大小
	SrcIp          string `json:"srcIp"`          // 源攻击IP地址
	DestIP         string `json:"destIP"`         // 目标IP地址
	BillId         string `json:"billId"`         // 事件工单编号
	FactoryId      int    `json:"factoryId"`      //事件来源
	AlertName      string `json:"alertName"`      //事件类别
	EventName      string `json:"eventName"`      //事件名称
	Devitype       string `json:"devitype"`       //安全设备类型
	Protectionarea string `json:"protectionarea"` // 安全防护区域
	DeviceIp       string `json:"deviceIp"`       //安全设备IP
}

type HVV struct {
	key    string
	aesKey string
	aesVi  string
}

func NewHvv() *HVV {
	hvv := HVV{}
	hvv.key = "S3DFGS98_gsd_3SDFGg6sG_fg"
	hvv.aesKey = "1234567890123456"
	hvv.aesVi = "1234567890123456"
	return &hvv
}

func (hvv *HVV) Encode(text string) string {
	//text := "{\"startTime\":\"2021-04-09 20:35:51\",\"endTime\":\"2021-04-09 21:35:51\",\"curPage\":1,\"pageSize\":10}"
	en, _ := AesEncryptCBC([]byte(text), []byte(hvv.aesKey), []byte(hvv.aesVi), ZeroPad)
	ent := base64.StdEncoding.EncodeToString(en)
	return url.QueryEscape(url.QueryEscape(ent))
}

func (hvv *HVV) EncodeJson(j interface{}) string {
	data, _ := json.Marshal(&j)
	return hvv.Encode(string(data))
}

func (hvv *HVV) SecurityAlarmQuery() {
	startTime, _ := time.ParseDuration("-1h")
	timeFormat := "2006-01-02 15:04:05"
	param := SecurityAlarmParam{
		PageSize:  1000,
		StartTime: time.Now().Add(startTime * 1).Format(timeFormat),
		EndTime: time.Now().Format(timeFormat),
		CurPage: 1,
	}

	paramJsonBy, _ := json.Marshal(&param)
	paramData := string(paramJsonBy)

	_url := "https://10.111.15.53:9998/itjk/api/securityAlarm/list"

	rep, err := grequests.Get(_url, &grequests.RequestOptions{
		Params: map[string]string{
			"params": paramData,
		},
		InsecureSkipVerify: true,
	})
	CheckErr(err)
	if rep.StatusCode != 200{
		fmt.Println("查询失败!")
	}
	ioutil.WriteFile("json.json", rep.Bytes(), 0777)
	rJson := rep.String()
	hvv.ExportExcel(gjson.Get(rJson, "datas").Array())
}

func (hvv *HVV) TestExportExcel(){
	s, _ := ioutil.ReadFile("json.json")
	hvv.ExportExcel(gjson.Get(string(s), "datas").Array())
}

func (hvv *HVV) ExportExcel(array []gjson.Result){
	f := excelize.NewFile()
	tIndex := f.NewSheet("报告")
	index := 1
	for _, v := range array{
		f.SetCellValue("报告", "A" + strconv.Itoa(index), v.Get("factory").String())
		f.SetCellValue("报告", "B" + strconv.Itoa(index), v.Get("deviceIp").String())
		f.SetCellValue("报告", "C" + strconv.Itoa(index), v.Get("lastoccursTime").String())
		f.SetCellValue("报告", "D" + strconv.Itoa(index), v.Get("alarmCount").String())
		f.SetCellValue("报告", "E" + strconv.Itoa(index), v.Get("srcIp").String())
		f.SetCellValue("报告", "F" + strconv.Itoa(index), v.Get("destIp").String())
		f.SetCellValue("报告", "G" + strconv.Itoa(index), v.Get("rowMessage").String())
		f.SetCellValue("报告", "H" + strconv.Itoa(index), v.Get("eventName").String())
		f.SetCellValue("报告", "I" + strconv.Itoa(index), v.Get("riskLevelCode").String())
		f.SetCellValue("报告", "J" + strconv.Itoa(index), v.Get("riskLevelName").String())
		f.SetCellValue("报告", "K" + strconv.Itoa(index), v.Get("devtypeName").String())
		f.SetCellValue("报告", "L" + strconv.Itoa(index), v.Get("protectionareaName").String())
		index += 1
	}
	f.SetActiveSheet(tIndex)
	if err := f.SaveAs("Export.xlsx"); err != nil {
		fmt.Println(err)
	}
}